package com.baogu.factoryjob.job.handler;

import com.baogu.factoryjob.service.BaoGuDataService;
import com.baogu.model.job.ErrorJob;
import com.baogu.model.job.where.ErrorJobWhere;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@DisallowConcurrentExecution
public class ErrorJobHand implements Job {

    @Autowired
    private SensordataJobHand sensordataJobHand;
    @Autowired
    private SensorWarningJobHand sensorWarningJobHand;
    @Autowired
    private BaoGuDataService baoGuDataService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        ErrorJobWhere errorJobWhere = new ErrorJobWhere();
        errorJobWhere.setState(0);
        List<ErrorJob> list = baoGuDataService.errorJobList(errorJobWhere);
        List<ErrorJob> errorJobsSensordata = new ArrayList<>();
        List<ErrorJob> errorJobsSensorWarning = new ArrayList<>();
        for (ErrorJob errorJob:list){
            if(errorJob.getType() == 3){
                errorJobsSensordata.add(errorJob);
            }
            else{
                errorJobsSensorWarning.add(errorJob);
            }
        }
        sensordataJobHand.execute(errorJobsSensordata);
        sensorWarningJobHand.execute(errorJobsSensorWarning);
    }
}
